package com.nttuan.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper
{
    private static final String TAG = "DatabaseHelper";
    private static String DB_PATH = "data/data/com.nttuan.mycalender/databases/";
    private static String DB_NAME = "calender_begin.db";
    private SQLiteDatabase myDataBase; 
    private final Context myContext;
    public DataBaseHelper(Context context) {
    	super(context, DB_NAME, null, 3);
        this.myContext = context;
        this.checkAndCopyDatabase();

    }
    @Override
	public void onCreate(SQLiteDatabase db) {
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
	@Override
	public synchronized void close() 
	{
    	    if(myDataBase != null)
                if(myDataBase.isOpen()) {
                    myDataBase.close();
                }
	}
	public void checkAndCopyDatabase()
	{
		boolean dbExist = checkDataBase();
    	if(dbExist){
    		Log.d(TAG, "database already exist!");
    	}
    	else
    	{
        	this.getReadableDatabase();
        	try 
        	{
    			copyDataBase();
    		} catch (IOException e) {
    			Log.e(TAG,"Error copying database");
        	}
    	}
	}
	private boolean checkDataBase()
	{
    	SQLiteDatabase checkDB = null;
    	try{
    		String myPath = DB_PATH + DB_NAME;
    		checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    	}
    	catch(SQLiteException e){}
    	if(checkDB != null) 
    		checkDB.close();
    	return checkDB != null ? true : false;
    }
	
	private void copyDataBase() throws IOException
	{
    	InputStream myInput = myContext.getAssets().open(DB_NAME);
    	String outFileName = DB_PATH + DB_NAME;
    	OutputStream myOutput = new FileOutputStream(outFileName);
    	byte[] buffer = new byte[1024];
    	int length;
    	while ((length = myInput.read(buffer))>0)
    	{
    		myOutput.write(buffer, 0, length);
    	}
    	myOutput.flush();
    	myOutput.close();
    	myInput.close();
    }
    public boolean insert(String tableName, ContentValues values){
        try{
            return myDataBase.insert(tableName,null,values) != -1;
        }catch (Exception e){
            Log.d(TAG,e.toString());
            return false;
        }
    }
    public boolean delete(String tableName, String whereClause, String[] whereArgs){
        try{
            return myDataBase.delete(tableName,whereClause,whereArgs) != 0 ;
        }catch (Exception e){
            Log.d(TAG,e.toString());
            return false;
        }
    }
    public  boolean update(String tableName, ContentValues values, String whereClause, String[] whereArgs){
        try{
            return myDataBase.update(tableName,values,whereClause,whereArgs) != 0;
        }catch (Exception e){
            Log.d(TAG,e.toString());
            return false;
        }
    }
    public boolean openDataBase()
    {
        try {
            String myPath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        }catch (Exception e){
            Log.d(TAG,e.toString());
            return false;
        }
        return true;
    }
    public boolean exeSQLData(String sql)
    {
        try {
            myDataBase.execSQL(sql);
            return true;
        }
        catch(Exception ex){
            Log.d(TAG,ex.toString());
            return false;
        }
    }
    public Cursor select(boolean distinct,String tableName,String[] columns,String selection,String[] args,String order){
        Cursor cursor = null;
        try {
         //   openDataBase();
            cursor = myDataBase.query(distinct, tableName, columns, selection, args, null, null, order, null);
        }catch (Exception e){
            Log.d(TAG,e.toString());
        }finally {
        //    this.close();
        }
        return cursor;
    }
    public Cursor queryData(String query)
    {
        Cursor cursor = null;
        try{
           cursor = myDataBase.rawQuery(query, null);
        }catch (Exception e){
            Log.d(TAG,e.toString());
        }finally {
            return cursor;
        }
    }
}
